package cn.itcast.bos.web.action.take_delivery;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FilenameUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.aspectj.util.FileUtil;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.web.action.common.BaseAction;

public class ImageAciton extends BaseAction<Object> {
	private File imgFile;
	private String imgFileFileName;
	private String imgFileContentType;

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

	public void setImgFileContentType(String imgFileContentType) {
		this.imgFileContentType = imgFileContentType;
	}

	// 文件上传
	@Action(value = "image_upload", results = { @Result(name = "success", type = "json") })
	public String imageUpload() throws IOException {
		System.out.println("文件:" + imgFile);
		System.out.println("文件名:" + imgFileFileName);
		System.out.println("文件类型:" + imgFileContentType);
		// 磁盘绝对路径
		String dirPath = "F:/temp/upload/";

		// 图片服务器地址
		String serverUrl = "http://localhost:8080/upload/";
		// 随机文件名
		String fileName = UUID.randomUUID().toString();

		// 扩展名 jpg png
		String ext = FilenameUtils.getExtension(imgFileFileName);
		// 拼接成完整的文件名
		fileName = fileName + "." + ext;
		// 保存文件
		FileUtil.copyFile(imgFile, new File(dirPath + fileName));
		Map<String,Object> result = new HashMap<>();
		// 存入值栈中,方便全部提取出来
		result.put("error", 0);
		result.put("url", serverUrl + fileName);

		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}

	// 展示图片服务器里的全部图片
	@Action(value = "image_manage", results = { @Result(name = "success", type = "json") })
	public String imageManage() {
		// 真实路径
		String rootPath = "F:/temp/upload/";
		String rootUrl = "http://localhost:8080/upload/";
		File directory = new File(rootPath);
		List<Map<String, Object>> fileList = new ArrayList<>();
		if (directory.listFiles() != null) {
			for (File file : directory.listFiles()) {
				Map<String, Object> hash = new HashMap<>();
				String fileName = file.getName();
				if (file.isDirectory()) {
					hash.put("is_dir", true);
					hash.put("has_file", (file.listFiles() != null));
					hash.put("filesize", 0L);
					hash.put("is_photo", false);
					hash.put("filetype", "");
				} else {
					String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
					hash.put("is_dir", false);
					hash.put("has_file", false);
					hash.put("filesize", file.length());
					hash.put("is_photo", true);
					hash.put("filetype", fileExt);
				}
				hash.put("filename", fileName);
				hash.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified()));
				fileList.add(hash);
			}
		}

		Map<String, Object> result = new HashMap<>();
		result.put("moveup_dir_path", "");
		result.put("current_dir_path", rootPath);
		result.put("current_url", rootUrl);
		result.put("total_count", fileList.size());
		result.put("file_list", fileList);

		ActionContext.getContext().getValueStack().push(result);
		return SUCCESS;
	}
}
